package com.dreamtour.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.DTO.ApplicationDTO;
import com.dreamtour.entity.Posts;
import com.dreamtour.service.IApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/applications")
public class ApplicationController {

    private IApplicationService applicationService;

    @Autowired
    public ApplicationController(IApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    @GetMapping("/list")
    public ResponseEntity<List<ApplicationDTO>> getApplications() {
        try {
            List<ApplicationDTO> applications = applicationService.getApplications();
            return ResponseEntity.ok(applications); // 返回 HTTP 200 和查询结果
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); // 处理异常
        }
    }

    @GetMapping("/getByPage")
    public IPage<ApplicationDTO> getByPage(Integer pageNum, Integer pageSize) {
        IPage<ApplicationDTO> page = applicationService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @GetMapping("/filter")
    public List<ApplicationDTO> filterApplications(
            @RequestParam(required = false) String applyCategory) {
        return applicationService.filterApplications(applyCategory);
    }

    @GetMapping("getById")
    public List<ApplicationDTO> getById(Integer userId) {
    	return applicationService.getByUserId(userId);
    }
}
